package com.lgy.utils;

import com.lgy.config.EsConfig;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class EsUtil {
    /**
     * 使用json文本的方式创建Index索引
     * @param indexName
     * @param indexContent
     */
    public static void createIndex(String indexName, String indexContent) {
        if(!indexExist(indexName)) {
            CreateIndexRequest request = new CreateIndexRequest(indexName); //创建索引
            try {
                request.source(indexContent,XContentType.JSON);
                CreateIndexResponse createIndexResponse = EsConfig.getEsInstance().indices().create(request, RequestOptions.DEFAULT);
                //返回的CreateIndexResponse允许检索有关执行的操作的信息，如下所示：
                boolean acknowledged = createIndexResponse.isAcknowledged();//指示是否所有节点都已确认请求
                boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();//指示是否在超时之前为索引中的每个分片启动了必需的分片副本数
                System.out.println(acknowledged + "::::::::::::::" + shardsAcknowledged);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 判断es索引是否存在
     * @param index
     * @return
     */
    public static boolean indexExist(String index) {
        GetIndexRequest request = new GetIndexRequest(index);
        request.local(false);
        request.humanReadable(true);
        request.includeDefaults(false);
        try {
            return EsConfig.getEsInstance().indices().exists(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 创建Document
     * @param json
     */
    public static void creatDoc(String indexName,String json){
        IndexRequest indexRequest = new IndexRequest(indexName);
        indexRequest.source(json, XContentType.JSON);
        try {
            IndexResponse indexResponse = EsConfig.getEsInstance().index(indexRequest, RequestOptions.DEFAULT);
            if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
                //处理创建文档的情况
                System.out.println("处理创建文档的情况：" + json);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改Document
     * @param json
     */
    public static void updateDoc(String _id,String indexName,String json){
        IndexRequest indexRequest = new IndexRequest(indexName);
        indexRequest.id(_id);
        indexRequest.source(json, XContentType.JSON);
        try {
            IndexResponse indexResponse = EsConfig.getEsInstance().index(indexRequest, RequestOptions.DEFAULT);
           if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
                //处理文档更新的情况
                System.out.println("处理文档更新的情况：" + json);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据主键ID查找
     * @param _id
     * @param indexName
     * @return
     */
    public static String findDocById(String _id,String indexName){
        GetRequest indexRequest = new GetRequest(indexName);
        indexRequest.id(_id);
        GetResponse response = null;
        String data = null;
        try {
            response = EsConfig.getEsInstance().get(indexRequest, RequestOptions.DEFAULT);
            data = response.getSourceAsString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 根据ID删除某个值
     * @param _id
     * @param indexName
     */
    public static void delById(String _id, String indexName) {
        DeleteRequest requestDel = new DeleteRequest(indexName, _id);
        // 2.发送请求
        try {
            EsConfig.getEsInstance().delete(requestDel, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
